package com.tianyi.sfzy1.controller;


import com.tianyi.sfzy1.domain.Audio;
import com.tianyi.sfzy1.service.AudioService;
import com.tianyi.sfzy1.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/audio")
public class AudioController {
    @Autowired
    private AudioService audioService;
//模糊查询
    @GetMapping("/list")
    public R findAll(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                     @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                      Audio audio) {
//        System.out.println(pageNum);
//        System.out.println(pageSize);
//        System.out.println(audio);
//        System.out.println(audioService.findByPage(pageNum, pageSize, audio));
        return R.ok(audioService.findByPage(pageNum, pageSize, audio));
    }
    @PostMapping
//    添加音频
    public R addAudio(@RequestBody Audio audio) {
        int i = audioService.addAudio(audio);
        if (i > 0) {
            return R.ok("添加成功");
        } else {
            return R.fail("添加失败");
        }
    }

    @DeleteMapping
//    删除音频
    public R deleteAudio( @RequestBody List<Integer> audioIds) {
        System.out.println("删除");
        System.out.println(audioIds);
        int i =  audioService.deleteAudios(audioIds);
        if (i > 0) {
            return R.ok("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }

    @GetMapping
//    查询音频
    public R findAllAudio(Long id) {
        System.out.println(id);
        Audio audio = audioService.findAllAudio(id);
        if (audio != null) {
            return R.ok(audio);
        } else {
            return R.fail("查询失败");
        }
    }

    @PutMapping
//    修改音频状态
    public R updateAudioStatus(@RequestBody  Audio audio) {
        int i = audioService.updateAudioStatus(audio);
        System.out.println(audio);
        if (i > 0) {
            return R.ok("修改成功");
        } else {
            return R.fail("修改失败");
        }
    }

    @GetMapping("/info")
//    查找音频和课程类型和咨询师相关联的表
    public R findAudioWithCourseTypeAndConsultant( @RequestParam(value = "title", required = false) String title, @RequestParam(value = "courseTypeName", required = false) String courseTypeName, @RequestParam(value = "consultantRealname", required = false) String consultantRealname, @RequestParam(value = "audioId", required = false) String audioId) {
        System.out.println(title);
        System.out.println(courseTypeName);
        System.out.println(consultantRealname);
        List<Audio> audioList = audioService.findAudioWithCourseTypeAndConsultantByFields(   title,
                courseTypeName,
                consultantRealname,
                audioId);
        if (audioList!= null) {
            return R.ok(audioList);
        } else {
            return R.fail("查询失败");
        }
    }
}
